#include "string.h"
void string_test()
{
    wanghao::string s("hello world");
    cout << s.c_str() << endl;
    cout << "s的大小为:" << s.size() << endl;
    //下标+[]遍历
    for(int i = 0;i < s.size();i++)
    {
        cout << s[i];
    }
    cout << endl;
    //迭代器遍历
    wanghao::string::iterator it = s.begin();
    while(it != s.end())
    {
        cout << *it;
        it++;
    }
    cout << endl;
    //插入一个字符
    cout << "插入一个字符" << endl;
    s.push_back('a');
    s.push_back('b');
    s.push_back('c');
    s.push_back('d');
    //范围for遍历,支持迭代器就支持范围for
    for(auto& e : s)
    {
        cout << e;
    }
    cout << endl;
}
void conststring_test()
{
    const wanghao::string s1("hello world wanghao");
    cout << s1.c_str() << endl;
    cout << "s1的大小为:" << s1.size() << endl;
    //下标+[]遍历
    for(int i = 0;i < s1.size();i++)
    {
        cout << s1[i];
    }
    cout << endl;
    //迭代器遍历
    wanghao::string::const_iterator it = s1.begin();
    while(it != s1.end())
    {
        cout << *it;
        it++;
    }
    cout << endl;
    //范围for遍历
    for(auto& e : s1)
    {
        cout << e;
    }
    cout << endl;
}
void StringTest1()
{
    //普通对象
    wanghao::string s1("hello");
    //const对象
    const wanghao::string s2("wanghao");
    for(const auto& e : s1)
    {
        cout << e;
    }
    cout << endl;
    cout << "插入数据前size:" << s1.size() << " " << "插入数据前capacity:" << s1.capacity() << endl; 
    s1.append(" hello wanghao");
    for(const auto& e : s1)
    {
        cout << e;
    }
    cout << endl;
        cout << "插入数据后size:" << s1.size() << " " << "插入数据后capacity:" << s1.capacity() << endl;
}
void StringTest2()
{
    //普通对象
    wanghao::string s1("hello world");
    for(const auto& e : s1)
    {
        cout << e;
    }
    cout << endl;
    //pos位置插入一个字符
    //s1.insert(s1.size(),"XXXXX");
    s1.erase(4,4);
    cout << s1.c_str() << endl;
}
void StringTest3()
{
    wanghao::string s("hello world");
    for(const auto& e : s)
    {
        cout << e;
    }
    cout << endl;
    int find = s.find("hello");
    if(find >= 0)
    {
        cout << "找到了,下标是:" << find << endl;
    }
    else cout << "没找到" << endl;
}
void StringTest4()
{
    wanghao::string s = "https://blog.csdn.net/m0_74271757?type=blog";
    //截取域名
    size_t s1 = s.find(':');
    size_t s2 = s.find('/',s1 + 3);
    if(s1 != string::npos && s2 != string::npos)
    {
        wanghao::string domain = s.substr(s1 + 3,s2 - (s1 + 3));
        cout << domain.c_str() << endl;
        wanghao::string uri = s.substr(s2 + 1);
        cout << uri.c_str() << endl;
    }


}
//拷贝构造测试&&赋值构造测试
void test()
{
    // wanghao::string s1("hello world");
    // wanghao::string s2 = s1;
    // cout << s1.c_str() << endl;
    // cout << s2.c_str() << endl;
    wanghao::string s1("hello world");
    wanghao::string s2("xxxxxxxxxxxxxxxx");
    s1 = s1;
    cout << s1.c_str() << endl;
    cout << s2.c_str() << endl;
}
void StringTest5()
{
    wanghao::string s("hello world");
    for(const auto& e : s)
    {
        cout << e;
    }
    cout << endl;
    s.clear();
    for(const auto& e : s)
    {
        cout << e;
    }
    cout << endl;
}
void StringTest6()
{
    wanghao::string s1("hello world");
    wanghao::string s2("wanghao");
    cout << s1 << "\n" << s2 << endl;
    //wanghao::swap(s1,s2);
    cout << s1 << "\n" << s2 << endl;
    cout << "strcmp" << strcmp(s1.c_str(),s2.c_str()) << endl;
    cout << "==:" << (s1 == s2) << endl;
    cout << "!=:" << (s1 != s2) << endl;
    cout << ">:" << (s1 > s2) << endl;
    cout << "<:" << (s1 < s2) << endl;
    cout << ">=:" << (s1 >= s2) << endl;
    cout << "<=:" << (s1 <= s2) << endl;
}
void StringTest7()
{
    wanghao::string s1;
    //cin >> s1;
    getline(cin,s1,'a');
    cout << s1 << endl;
}
void __string__test_()
{
    cout << "创建对象1&&对象2" << endl;
    wanghao::string s1 = "hello world";
    wanghao::string s2 = "Welcome";
    cout << "范围for遍历s1:";
    for(const auto& e : s1)
    {
        cout << e;
    }
    cout << endl;
    cout << "迭代器遍历s2:";
    wanghao::string::iterator it = s1.begin();
    while(it != s1.end())
    {
        cout << *it;
        it++;
    }
    cout << endl;
    cout << "s1中尾插一个字符串....." << endl;
    s1.append(" Mr.Zhao");
    cout << "下标遍历s1:";
    for(int i = 0;i < s1.size();i++)
    {
        cout << s1[i];
    }
    cout << endl;
    cout << "s2中尾插一个字符串....." << endl;
    s1.append(" Zhao to study at Bit");
    cout << "下标遍历s2:";
    for(int i = 0;i < s1.size();i++)
    {
        cout << s1[i];
    }
    cout << endl;
}
int main()
{
    // //普通对象
    // string_test();
    // cout << "-------------------------------" << endl;
    // //const对象
    // conststring_test();
    //StringTest7();
    //test();
    __string__test_();
    return 0;
}
